Skip to content

Add lastError function#3654

Merged
d-torrance merged 7 commits intoMacaulay2:developmentfrom
d-torrance:last-error
May 16, 2025
Merged

Add lastError function#3654
d-torrance merged 7 commits intoMacaulay2:developmentfrom
d-torrance:last-error

Conversation

@d-torrance
Copy link
Copy Markdown
Member

This returns a sequence containing the location and message string for the last error (at least most of the time, e.g., not for syntax errors).

The eventual goal is to use this in the MPI package (see #2129) for handling errors gracefully, e.g., doing something like try x else lastError().

e(x,p1,h1)
f(x,p1,h1)
g(x,p1,h1)
assert((last try 1/0 else lastError()) == "division by zero")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning on replacing the manual tests above with automatic versions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

@pzinn
Copy link
Copy Markdown
Contributor

pzinn commented Feb 13, 2025

I did something similar on my dev branch. I'll compare the code.

Comment thread M2/Macaulay2/d/expr.d
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not directly related, but do you know what these two lines are for:

M2/M2/Macaulay2/d/expr.d

Lines 307 to 308 in 3fc8759

export errorMessageClass := newtypeof(basicListClass);
export missingMethodClass := newtypeof(errorMessageClass);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea! They don't appeared to be used anywhere.

@pzinn
Copy link
Copy Markdown
Contributor

pzinn commented Feb 26, 2025

here are AFAICT the main differences I find between your implementation and mine:

  • I update the last error message when it's printed, not when it's built.
  • I define a new global variable lastError (well, I didn't call it that...) rather than a function lastError(). On that last point, can you comment on why this is better?

@d-torrance
Copy link
Copy Markdown
Member Author

  • I update the last error message when it's printed, not when it's built.

One of the big goals was to find out if there was an error without actually raising one, e.g., try foo() else lastError() would tell us what happened if foo() raised and error without actually raising an error.

That being said, @MichaelABurr has some code updating it during printErrorMessage. See d-torrance#13. Is that what you were doing, too?

  • I define a new global variable lastError (well, I didn't call it that...) rather than a function lastError(). On that last point, can you comment on why this is better?

That was our original idea, but we didn't realize it was possible. Most of the global variables like this (e.g., printWidth) seem to be set at top-level and accessed in the interpreter. Whereas this would be set in the interpreter and accessed at top-level. We couldn't figure out an obvious way to do that, hence the function.

Is your code live on Macaulay2Web? Or just in a branch somewhere?

@pzinn
Copy link
Copy Markdown
Contributor

pzinn commented Feb 26, 2025

good point. maybe I'm wrong about updating it when it's printed, as you said, the main goal is to test the error message with try. Yes it is live on M2Web, but it's not working any longer, I made this at the same time as my (failed) error upgrade exactly a year ago, and since then changes to the official development branch broke it; try viewHelp "try", the example there doesn't work any more.

@pzinn
Copy link
Copy Markdown
Contributor

pzinn commented Feb 26, 2025

the relevant commit seems to be 74e022a
yeah it's clumsy, it updates the error message when it's printed or when try is called (which I'm assuming is the part that got overwritten by a subsequent PR).

@pzinn
Copy link
Copy Markdown
Contributor

pzinn commented Feb 26, 2025

I still think having a global variable is marginally simpler than a function.
Ah, another thing is I also defined the location of the last error. Not sure if it belongs in your PR.
The goal for me was to transfer all the error printing to the m2 level.

@mahrud
Copy link
Copy Markdown
Member

mahrud commented Mar 9, 2025

It might be nice to have something another variable to remember the value of current after an error has occurred.

@d-torrance
Copy link
Copy Markdown
Member Author

I've rewritten this a bit so that lastError is a variable, like Paul's version, that we can clear by assigning null:

i1 : 1/0
stdio:1:1:(3): error: division by zero

i2 : lastError

o2 = (stdio:1:0-1:3, division by zero)

o2 : Sequence

i3 : lastError = null

@d-torrance d-torrance marked this pull request as ready for review March 18, 2025 04:08
Comment thread M2/Macaulay2/d/parser.d
printErrorMessage(tryToken," ... to match this 'try'");
return errorTree);
makeParseError(elseToken,"syntax error : expected 'else'");
return makeParseError(tryToken," ... to match this 'try'"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what's the meaning of these changes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few constructors for lower-level lexing and parsing errors that set lastError in these cases.

@d-torrance d-torrance merged commit b580c28 into Macaulay2:development May 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants